﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class NeedMaterialListForPurchaseOrderHeader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                var bll = mk.l.Sales_NeedMaterialHeader.ToList();
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Single(h => h.ClassID == (int.Parse(currentUser.DepartmentID))).RootID.ToString();
                //只取出StatusSubmit为2 即设计师已经提交到财务的订单列表
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text + " and StatusSubmit='2'";
                // this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");

            }
        }
        private void BindData()
        {
            var bll = mk.l.Sales_NeedMaterialHeader.ToList();
            //  DataSet ds = bll.GetList(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, this.lblStrWhere.Text, "1");
            this.myGridView.DataSource = bll;
            this.myGridView.DataBind();
        }
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            var bllNeedHeader = mk.l.Sales_NeedMaterialHeader.ToList();
            var bllOrderHeader = mk.l.Sales_SalesOrderHeader.ToList();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //取得订货单信息
                Sales_NeedMaterialHeader mNeedHeader = bllNeedHeader.Where(h => h.NeedMaterialID == int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["NeedMaterialID"].ToString())).FirstOrDefault();
                //取得订单信息
                Sales_SalesOrderHeader mOrderHeader = bllOrderHeader.Where(h => h.SalesOrderID == mNeedHeader.SalesOrderID).FirstOrDefault();
                //显示客户类型
                Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
                var bllC = mk.l.Sales_Customer.ToList();
                Sales_Customer mC = bllC.Where(h => h.CustomerID == mOrderHeader.CustomerID).FirstOrDefault();
                if (mC != null)
                {
                    lblCustomerName.Text = mC.CustomerName;
                }
                //显示订单编号
                Label lblSalesOrderNumber = (Label)(e.Row.FindControl("lblSalesOrderNumber"));
                lblSalesOrderNumber.Text = mOrderHeader.SalesOrderNumber;
                //显示设计师姓名
                Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
                var bllUser = mk.l.Accounts_Users.ToList();
                Accounts_Users mUser = bllUser.Where(h => h.UserName == mOrderHeader.Designer).First();
                lblDesigner.Text = mUser.TrueName;

                //显示提交状态
                Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
                string strStatusSubmit = mNeedHeader.StatusSubmit;
                switch (strStatusSubmit)
                {
                    case "1":
                        lblStatusSubmit.Text = "初始添加";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusSubmit.Text = "未知";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                ////显示付款状态
                //Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
                //string strStatusPayment = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPayment"].ToString();
                //switch (strStatusPayment)
                //{
                //    case "1":
                //        lblStatusPayment.Text = "未付款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                //        break;
                //    case "2":
                //        lblStatusPayment.Text = "未付全款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Black;
                //        break;
                //    case "3":
                //        lblStatusPayment.Text = "已付全款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Green;
                //        break;
                //    default:
                //        lblStatusPayment.Text = "未知";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                //        break;
                //}

                //显示订货状态 同时根据订货状态 确定编辑链接是Creat还是Edit
                Label lblStatusPurchaseMaterial =
                    (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
                //得到超级链接
                HyperLink linkPurchaseOrderEdit =
                    (HyperLink)e.Row.FindControl("linkPurchaseOrderEdit");
                //判断NeedMaterialHeader实体中是否已创建订货单的状态
                string strStatusPurchaseMaterial = mNeedHeader.StatusPurchaseMaterial;
                //得到订货单的ID值
                string NeedMaterialID =
                    this.myGridView.DataKeys[e.Row.RowIndex].Values
                    ["NeedMaterialID"].ToString();
                switch (strStatusPurchaseMaterial)
                {
                    case "1":        //未订货状态时，设置导航链接到创建页面
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        linkPurchaseOrderEdit.NavigateUrl =
                        "PurchaseOrderHeaderCreatFromNeedMaterial.aspx?NeedMaterialID=" + NeedMaterialID;
                        linkPurchaseOrderEdit.Text = "添加";
                        break;
                    case "2":        //已订货状态时，设置导航链接到编辑页面
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                        linkPurchaseOrderEdit.NavigateUrl =
                        "PurchaseOrderHeaderEditFromNeedMaterial.aspx?NeedMaterialID=" + NeedMaterialID;
                        linkPurchaseOrderEdit.Text = "编辑";
                        break;
                    default:       //未知状态，不设置任何链接
                        lblStatusPurchaseMaterial.Text = "未知";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                ////显示流程状态
                //Label lblStatusAll = (Label)(e.Row.FindControl("lblStatusAll"));
                //string strStatusAll = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusAll"].ToString();
                //switch (strStatusAll)
                //{
                //    case "1":
                //        lblStatusAll.Text = "未完成";
                //        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                //        break;
                //    case "2":
                //        lblStatusAll.Text = "已完成";
                //        lblStatusAll.ForeColor = System.Drawing.Color.Green;
                //        break;
                //    default:
                //        lblStatusAll.Text = "未知";
                //        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                //        break;
                //}

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {

        }

    }
}